package com.quick.mybatis.model;

import java.io.Serializable;
import java.util.List;

import com.quick.mybatis.utils.StringUtil;

public final class PageModel<T> implements Serializable {
	
	private String beginDate;
	private String endDate;

	private List<T> result;
	
	private int maxPage;
	
	private String where;

	public String getWhere() {
		if(StringUtil.isEmpty(this.where)){
			return "";
		}
		return where;
	}

	public void setWhere(String where) {
		this.where = where;
	}

	public PageModel(List<T> result, int maxPage) {
		this.result = result;
		this.maxPage = maxPage;
	}
	
	private static final long serialVersionUID = 5472321653620726832L;  
	  
    private final static int DEFAULT_NAVIGATOR_SIZE = 5;  
  
    //当前页  
    private int currentPage = 1;  
    //每页显示数量  
    private int pageSize = 10;  
      
    //总条数  
    private int totalCount;  
  
    private boolean havaNextPage;  
  
    private boolean havePrePage;  
  
    private int navigatorSize;  
      
    //存放查询结果用的list  
    private List<T> items;  
    
    private T item;
    
    public T getItem() {
		return item;
	}

	public void setItem(T item) {
		this.item = item;
	}

	public PageModel(){  
          
    }  
      
    public PageModel(int totalCount, int pageSize, int currentPage) {  
        this(totalCount, pageSize, currentPage, DEFAULT_NAVIGATOR_SIZE);  
    }  
  
    public PageModel(int totalCount, int pageSize, int currentPage,  
                        int navigatorSize) {  
        this.totalCount = totalCount;  
        this.pageSize = pageSize;  
        this.currentPage = currentPage;  
        this.navigatorSize = navigatorSize;  
    }  
      
    public int getPageCount() {  
        int pageCount = 0;  
        if (pageSize != 0) {  
            pageCount = totalCount / pageSize;  
            if (totalCount % pageSize != 0)  
                pageCount++;  
        }  
  
        return pageCount;  
    }  
  
    public int getCurrentPage() {  
        currentPage = currentPage < getPageCount() ? currentPage :  
                      getPageCount();  
        currentPage = currentPage < 1 ? 1 : currentPage;  
  
        return currentPage;  
    }  
    
    public Integer getStartNum(){
    	return (this.getCurrentPage() -1) * pageSize;
    }
  
    public int getPageSize() {  
        return pageSize;  
    }  
  
    public int getTotalCount() {  
        return totalCount;  
    }  
  
  
    public boolean isHaveNextPage() {  
        havaNextPage = false;  
        if ((getPageCount() > 1) && (getPageCount() > getCurrentPage()))  
            havaNextPage = true;  
        return havaNextPage;  
    }  
  
    public boolean isHavePrePage() {  
        havePrePage = false;  
        if ((getPageCount() > 1) && (currentPage > 1))  
            havePrePage = true;  
        return havePrePage;  
    }  
  
    private int getNavigatorIndex(boolean isBegin) {  
        int beginNavigatorIndex = getCurrentPage() - navigatorSize / 2;  
        int endNavigatorIndex = getCurrentPage() + navigatorSize / 2;  
        beginNavigatorIndex = beginNavigatorIndex < 1 ? 1 : beginNavigatorIndex;  
        endNavigatorIndex = endNavigatorIndex < getPageCount() ?  
                            endNavigatorIndex :  
                            getPageCount();  
        while ((endNavigatorIndex - beginNavigatorIndex) < navigatorSize &&  
               (beginNavigatorIndex != 1 || endNavigatorIndex != getPageCount())) {  
            if (beginNavigatorIndex > 1)  
                beginNavigatorIndex--;  
            else if (endNavigatorIndex < getPageCount())  
                endNavigatorIndex++;  
        }  
  
        if(isBegin)  
            return beginNavigatorIndex;  
        else  
            return endNavigatorIndex;  
    }  
  
    public int getBeginNavigatorIndex() {  
        return getNavigatorIndex(true);  
    }  
  
    public int getEndNavigatorIndex() {  
        return getNavigatorIndex(false);  
    }  
  
    public List<T> getItems() {  
        return items;  
    }  
  
    public void setItems(List<T> items) {  
        this.items = items;  
    }  
      
    public void setCurrentPage(int currentPage) {  
        this.currentPage = currentPage;  
    }  
  
    public void setPageSize(int pageSize) {  
        this.pageSize = pageSize;  
    }  
  
    public void setTotalCount(int totalCount) {  
        this.totalCount = totalCount;  
    }  
  
    
    public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public int getMaxPage() {
		return maxPage;
	}

	public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	
}
